package org.ctstudio.oa.duty.web;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ctstudio.oa.duty.bean.AbnormalSignApply;
import org.ctstudio.oa.duty.bean.Apply;
import org.ctstudio.oa.duty.bean.ApplyQuery;
import org.ctstudio.oa.duty.bean.SignRecord;
import org.ctstudio.oa.duty.dao.AbnormalSignMapper;
import org.ctstudio.oa.duty.dao.SignRecordsMapper;
import org.ctstudio.oa.duty.service.DutyCalcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 请假业务
 * 
 * @author ct
 *
 */
@Controller
@RequestMapping("/abnormalsign")
public class AbnormalSignController extends AbstractApplyController {
  @Autowired
  private AbnormalSignMapper mapper;

  @RequestMapping("/list.do")
  public String list(@ModelAttribute("query") ApplyQuery query, Model model) {
    long total = mapper.count(query);
    model.addAttribute("total", total);
    if (total > 0) {
      List<AbnormalSignApply> list = mapper.query(query);
      model.addAttribute("list", list);
      model.addAttribute("staffMap", this.getStaffIdNameMap(list));
    }
    return "abnormalSign/list";
  }

  @ModelAttribute("signTypes")
  public Map<Long, String> getSignTypes() {
    return getSettings("sign.types");
  }

  @RequestMapping(value = "/apply.do", method = RequestMethod.GET)
  public String apply(Model model) {
    model.addAttribute("apply", new AbnormalSignApply());
    return "abnormalSign/newOrEdit";
  }

  @RequestMapping(value = "/apply.do", method = RequestMethod.POST)
  public String onApply(@ModelAttribute("apply") AbnormalSignApply apply, BindingResult result, Model model, RedirectAttributes redirect) {
    apply.setStatus(1);
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "uid", "CANNOTEMPTY");
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "abnormalDate", "CANNOTEMPTY");
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "signType", "CANNOTEMPTY");
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "actualSignTime", "CANNOTEMPTY");
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "reason", "CANNOTEMPTY");
    if (result.hasErrors()) {
      return "abnormalSign/newOrEdit";
    }

    if (apply.isNew()) {
      mapper.add(apply);
    } else {
      mapper.edit(apply);
    }
    return redirectList(apply, redirect);
  }

  private String redirectList(AbnormalSignApply apply, RedirectAttributes redirect) {
    LocalDate date = apply.getAbnormalDate();
    redirect.addAttribute("y", date.getYear());
    redirect.addAttribute("m", date.getMonthValue());
    redirect.addAttribute("d", date.getDayOfMonth());
    return "redirect:list.do?y={y}&m={m}&d={d}";
  }

  @RequestMapping(value = "/detail.do")
  public String detail(@RequestParam Long id, Model model) {
    model.addAttribute("info", mapper.get(id));
    return "abnormalSign/detail";
  }

  @RequestMapping(value = "/approve.do", method = RequestMethod.GET)
  public String approve(@RequestParam long id, Model model) {
    model.addAttribute("apply", mapper.get(id));
    return "abnormalSign/approve";
  }

  @RequestMapping(value = "/approve.do", method = RequestMethod.POST)
  public String onApprove(@ModelAttribute("approval") Apply approval, Model model, HttpServletRequest request,
      RedirectAttributes redirect) {
    approval.setApprover(request.getRemoteUser());
    mapper.approve(approval);
    AbnormalSignApply apply = mapper.get(approval.getId());
    if (apply.getStatus() == Apply.STATUS_PASS) {
      refreshDuty(apply);
    }
    model.addAttribute("apply", apply);
    return redirectList(apply, redirect);
  }

  @Autowired
  private DutyCalcService dutyService;

  @Autowired
  private SignRecordsMapper signRecordMapper;

  /**
   * 忘打卡申请通过后刷新考勤记录
   * 
   * @param apply
   */
  private void refreshDuty(AbnormalSignApply apply) {
    SignRecord sign = new SignRecord();
    sign.setUid(apply.getUid());
    sign.setSignDate(apply.getAbnormalDate());
    sign.setSignTime(apply.getActualSignTime());
    signRecordMapper.abnormalAdd(sign);
    dutyService.refreshDuty(apply.getAbnormalDate(), apply.getUid());
  }
}
